package com.mall.user.services;


import com.mall.commons.tool.exception.ValidateException;
import com.mall.user.ILoginService;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.converter.MemberConverter;
import com.mall.user.dal.entitys.Member;
import com.mall.user.dal.persistence.MemberMapper;
import com.mall.user.dto.*;
import com.mall.user.utils.AESUtil;
import com.mall.user.utils.JwtTokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.Objects;


/**
 * @author : [Dragon]
 * @createTime : [2022/1/20 15:48]
 * @description :用户登录
 */

@Slf4j
@Component
@Service
public class LoginServiceImpl implements ILoginService {

    @Autowired
    MemberMapper memberMapper;

    @Autowired
    MemberConverter memberConverter;

    @Override
    public CheckAuthResponse validToken(CheckAuthRequest checkAuthRequest) {
        String token = checkAuthRequest.getToken();
        CheckAuthResponse checkAuthResponse = new CheckAuthResponse();
        String userId = null;
        try {
            userId = JwtTokenUtils.builder().token(token).build().freeJwt();
        } catch (ValidateException e) {
            checkAuthResponse.setCode(SysRetCodeConstants.TOKEN_VALID_FAILED.getCode());
            checkAuthResponse.setMsg(SysRetCodeConstants.TOKEN_VALID_FAILED.getMessage());
        }
        checkAuthResponse.setCode(SysRetCodeConstants.SUCCESS.getCode());
        checkAuthResponse.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
        checkAuthResponse.setUserinfo(userId);
        return checkAuthResponse;
    }

    @Override
    public UserLoginResponse validUserLoign(UserLoginRequest userLoginRequest) {
        String userName = userLoginRequest.getUserName();
        String userPwd = userLoginRequest.getUserPwd();
        userPwd = new AESUtil(userPwd).encrypt();
        //根据username，password查询
        Example example = new Example(Member.class);
        example.createCriteria().andEqualTo("username", userName);

        Member member = memberMapper.selectOneByExample(example);
        //判断是否用户激活
        if (member.getIsVerified().equals("N")) {
            UserLoginResponse response = new UserLoginResponse();
            response.setCode("500");
            response.setMsg("邮箱未激活");
            return response;
        }

        //
        if (Objects.equals(member.getPassword(), userPwd)) {
            //加密
            String token = JwtTokenUtils.builder().msg(member.getId().toString()).build().creatJwtToken();
            UserLoginResponse userLoginResponse = memberConverter.selectMenberToResp(member);
            //将token放入返回类中
            log.info("token中当前的userId为: " + member.getId());
            userLoginResponse.setToken(token);
            userLoginResponse.setCode("200");
            userLoginResponse.setMsg("登录成功");
            return userLoginResponse;
        }

        UserLoginResponse response = new UserLoginResponse();
        response.setCode("500");
        response.setMsg("账号密码错误");
        return response;
    }


    @Override
    public UserLoginGetResponse getLoginMsg(String userId) {
        Member member = memberMapper.selectByPrimaryKey(userId);
        UserLoginGetResponse userLoginGetResponse = memberConverter.selectMenberToRespGet(member);
        return userLoginGetResponse;
    }
}
